package com.lvhx.controller;

import com.lvhx.dao.extend.OrderExtednMapper;
import com.lvhx.task.CallableTask;
import com.lvhx.task.RunnableTask;
import com.lvhx.task.TestJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
@Slf4j
@Api(tags = "多线程测试")
@RestController
@RequestMapping("/thread")
public class ThreadController {
    @Resource(name = "threadPool")
    private ExecutorService threadPool;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private OrderExtednMapper orderExtednMapper;

    @ApiOperation("测试线程池")
    @RequestMapping(value = {"/testThreadPool"}, produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String testThreadPool() {
        CallableTask testTask = new CallableTask();
        Future future = threadPool.submit(testTask);
        new Thread(new RunnableTask()).start();

        System.out.println("结果已经返回");
        return "success";
    }

    @ApiOperation("测试子线程抛错")
    @RequestMapping(value = {"/test1"}, method = RequestMethod.GET)
    public void test1() {

        List<Future> list = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            TestJob testJob = new TestJob();
            testJob.setName("thread-" + i);
            Future<String> future = threadPoolTaskExecutor.submit(testJob);
//            threadPoolTaskExecutor.submit(testJob);
            list.add(future);
        }

        //　主线程如果不主动获取子线程结果，子线程的跑错不会打印出来
//        for (Future future : list) {
//            try {
//                future.get();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
    }

    @ApiOperation("测试mapper.xml里for循环套for循环")
    @RequestMapping(value = {"/test2"}, method = RequestMethod.GET)
    public void test2() {
        List<String> columns = new ArrayList<>();
        columns.add("id");
        columns.add("name");
        columns.add("message_id");

        List<Map<String, Object>> dataList = new ArrayList<>();

        Map<String, Object> map1 = new java.util.HashMap<>();
        map1.put("id", UUID.randomUUID().toString().replace("-",""));
        map1.put("name", UUID.randomUUID().toString().replace("-",""));
        map1.put("message_id", "message_id1");

        Map<String, Object> map2 = new java.util.HashMap<>();
        map2.put("id",UUID.randomUUID().toString().replace("-",""));
        map2.put("name", UUID.randomUUID().toString().replace("-",""));
        map2.put("message_id", "message_id2");

        Map<String,Object> map3 = new java.util.HashMap<>();
        map3.put("id", UUID.randomUUID().toString().replace("-",""));
        map3.put("name", UUID.randomUUID().toString().replace("-",""));
        map3.put("message_id", "message_id3");

        dataList.add(map1);
        dataList.add(map2);
        dataList.add(map3);

        orderExtednMapper.insertBatchByTableName("t_order", columns, dataList);
    }

    @ApiOperation("测试主线程等待")
    @RequestMapping(value = {"/test3"}, method = RequestMethod.GET)
    public void test3() {
        List<Future> list = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            TestJob testJob = new TestJob();
            testJob.setName("thread-" + i);
            Future<String> future = threadPoolTaskExecutor.submit(testJob);
//            threadPoolTaskExecutor.submit(testJob);
            list.add(future);
        }

        //　主线程如果不主动获取子线程结果，主线程会直接结束，如果获取结果，则等待结果后再结束
        for (Future future : list) {
            try {
                future.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        log.info("=====主线程结束");

    }

    @ApiOperation("测试子线程使用缓存mapper")
    @RequestMapping(value = {"/test4"}, method = RequestMethod.GET)
    public void test4() {
        List<Future> list = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            TestJob testJob = new TestJob();
            testJob.setName("thread-" + i);
            Future<String> future = threadPoolTaskExecutor.submit(testJob);
//            threadPoolTaskExecutor.submit(testJob);
            list.add(future);
        }

        //　主线程如果不主动获取子线程结果，主线程会直接结束，如果获取结果，则等待结果后再结束
        for (Future future : list) {
            try {
                future.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
